package com.smarth.zhizulian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.app.EventActivity;
import com.smarth.zhizulian.bean.BaseEntity;
import com.smarth.zhizulian.bean.cash.CashIsFirstCash;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.bad.BadPageObserver;
import com.smarth.zhizulian.http.observer.ProgressObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.popupwindow.GetOneDialog;
import com.smarth.zhizulian.popupwindow.MessageDialog;
import com.smarth.zhizulian.utils.CommonUtils;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.utils.listhelper.RefreshHelper;
import com.smarth.zhizulian.utils.listhelper.impl.adapter.PageDataAdapter;
import com.smarth.zhizulian.utils.listhelper.impl.model.HttpPageDataModel;
import com.smarth.zhizulian.view.LoadingLayout;
import com.smarth.zhizulian.view.MyRadioGroup;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

/**
 * Created by 18865 on 2017/12/28.
 */

public class GetMoneyActivity extends EventActivity {
    @BindView(R.id.iv_one)
    ImageView ivOne;
    @BindView(R.id.rg_radiogroup)
    MyRadioGroup rgRadiogroup;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.ll_loadinglayout)
    LoadingLayout llLoadinglayout;
    @BindView(R.id.tv_shouxu)
    TextView tvShouxu;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.iv_getmoney)
    ImageView ivGetmoney;
    private CashIsFirstCash mCashIsFirstCash;
    private RefreshHelper<CashIsFirstCash> refreshHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_getmoney);
        ButterKnife.bind(this);
        setTitlebar("立即提现", true);
        rgRadiogroup.check(R.id.rb_1);
        rgRadiogroup.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(MyRadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_moneyone:
                        tvShouxu.setText("提现手续费：0.01元");
                        break;
                    case R.id.rb_1:
                        tvShouxu.setText("提现手续费：0.10元");
                        break;
                    case R.id.rb_2:
                        tvShouxu.setText("提现手续费：0.20元");
                        break;
                    case R.id.rb_3:
                        tvShouxu.setText("提现手续费：0.50元");
                        break;
                    case R.id.rb_4:
                        tvShouxu.setText("提现手续费：1.00元");
                        break;
                }
                handleLeft();
            }
        });
        initIndex();
    }

    private void initIndex() {
        refreshHelper = new RefreshHelper<>(mContext, llLoadinglayout, null);
        refreshHelper.setDataModel(new HttpPageDataModel<CashIsFirstCash>(mContext) {
            @Override
            public Observable<CashIsFirstCash> getObservable() {
                return KumaHttp.getService().cashIsFirstCash()
                        .compose(RxCompos.<CashIsFirstCash>getBELoginCompos());
            }
        });
        refreshHelper.setDataAdapter(new PageDataAdapter<CashIsFirstCash>() {
            @Override
            public void onGetData(CashIsFirstCash cashIsFirstCash, boolean isRefresh) {
                super.onGetData(cashIsFirstCash, isRefresh);
                mCashIsFirstCash = cashIsFirstCash;
                tvLeft.setText("余额：" + cashIsFirstCash.getMoeny() + "元");
                if (!TextUtils.isEmpty(cashIsFirstCash.getName())) {
                    etName.setText(cashIsFirstCash.getName());
                }
                handleLeft();
            }
        });
        refreshHelper.refresh();
    }

    public static void open(Context context) {
        Intent i = new Intent(context, GetMoneyActivity.class);
        context.startActivity(i);
    }

    @OnClick({R.id.iv_one, R.id.iv_getmoney, R.id.iv_copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_one:
                fetchGetMoney(1);
                break;
            case R.id.iv_getmoney:
                int money = 10;
                switch (rgRadiogroup.getCheckedRadioButtonId()) {
                    case R.id.rb_moneyone:
                        money = 1;
                        break;
                    case R.id.rb_1:
                        money = 10;
                        break;
                    case R.id.rb_2:
                        money = 20;
                        break;
                    case R.id.rb_3:
                        money = 50;
                        break;
                    case R.id.rb_4:
                        money = 100;
                        break;
                }
                fetchGetMoney(money);
                break;
            case R.id.iv_copy:
                CommonUtils.copyToClipboard(mContext, "BZ1226372200");
                break;
        }
    }

    private void fetchGetMoney(int money) {
        KumaHttp.getService().userCashApply(etName.getText().toString(), "" + money)
                .compose(RxCompos.getBEAllLoginCompos())
                .subscribe(new ProgressObserver<BaseEntity<Object>>(mContext) {
                    @Override
                    public void onNext(BaseEntity<Object> objectBaseEntity) {
                        new MessageDialog(mContext)
                                .setMessage("提现申请已提交！\n请留意请微信钱包收入")
                                .setLeft("我知道了", null)
                                .setRight("继续阅读", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        MainActivity.openClearTask(mContext, MainActivity.MODE_NORMAL);
                                    }
                                }).show();
                        EventBus.getDefault().post(new EventBean(mClassName), BaseConfig.EventTag.ON_GETMONEY);
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        if (TextUtils.isEmpty(etName.getText().toString())) {
                            KumaToast.show(mContext, "请输入真实姓名");
                            d.dispose();
                            return;
                        }
                        super.onSubscribe(d);
                    }

                    @Override
                    public void onErrorApi(BaseEntity be) {
                        if (BaseConfig.NetWork.CODE_GETONE.equals(be.getCode())) {
                            new GetOneDialog(mContext).show();
                            return;
                        }
                        super.onErrorApi(be);
                    }

                });
    }

    @Subscriber(tag = BaseConfig.EventTag.ON_GETMONEY)
    private void onGetMoney(EventBean eb) {
//        fetchIndex();
        refreshHelper.loadMore();
    }

    private void handleLeft() {
        double money = 0;
        switch (rgRadiogroup.getCheckedRadioButtonId()) {
            case R.id.rb_moneyone:
                money = 1.01;
                break;
            case R.id.rb_1:
                money = 10.1;
                break;
            case R.id.rb_2:
                money = 20.2;
                break;
            case R.id.rb_3:
                money = 50.5;
                break;
            case R.id.rb_4:
                money = 101;
                break;
        }
        if (Double.valueOf(mCashIsFirstCash.getMoeny()) < money) {
            ivGetmoney.setEnabled(false);
        } else {
            ivGetmoney.setEnabled(true);
        }
    }
}
